package com.z2k.todo.controllers;

import com.z2k.todo.entity.Todo;
import com.z2k.todo.entity.TodoClass;
import com.z2k.todo.page.TodoClassVO;
import com.z2k.todo.page.TodoVO;
import com.z2k.todo.services.TodoService;
import com.z2k.todo.services.impl.TodoServiceImpl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

public class TodoController {

    // xxx/todo/listtodoclasses.wst
    public TodoVO listtodoclasses(HttpServletRequest req, HttpServletResponse resp){
        TodoService todoService = new TodoServiceImpl();
        List<TodoClassVO> result = todoService.listTodoClasses(null);
        TodoVO todoVO = new TodoVO();
        todoVO.setTodoclasses(result);
        return todoVO;
    }

    // xxx/todo/savetodoclass.wst
    public Boolean savetodoclass(HttpServletRequest req,HttpServletResponse resp){
        TodoService todoService = new TodoServiceImpl();
        TodoClass todoClass = new TodoClass();
        todoClass.setName(req.getParameter("todoclassname"));
        System.out.println("名称："+req.getParameter("todoclassname"));
        Boolean result = todoService.saveTodoClass(todoClass);
        return result;
    }

    // xxx/todo/savetodo.wst
    public Boolean savetodo(HttpServletRequest req,HttpServletResponse resp){
        TodoService todoService = new TodoServiceImpl();
        Todo todo = new Todo();
        Date date = new Date();
        todo.setCreate_dt(date);
        todo.setState(0);
        todo.setTitle(req.getParameter("title"));
        todo.setTodoclassid(Integer.valueOf(req.getParameter("todoclassid")));
        Boolean result = todoService.saveTodo(todo);
        return result;
    }

    // xxx/todo/updatetodo.wst
    public Boolean updatetodo(HttpServletRequest req,HttpServletResponse resp){
       TodoService todoService = new TodoServiceImpl();
       Boolean result = todoService.updateTodo(Integer.valueOf(req.getParameter("id")));
       return result;
    }

    // xxx/todo/deletetodo.wst
    public Boolean deletetodo(HttpServletRequest req,HttpServletResponse resp){
        TodoService todoService = new TodoServiceImpl();
        Boolean result = todoService.deletetodo(Integer.valueOf(req.getParameter("id")));
        System.out.println("id:"+Integer.valueOf(req.getParameter("id")));
        return result;
    }
}
